package com.zwb.demo.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.poifs.filesystem.FileMagic;

import java.io.*;
import java.util.Objects;

public class MyExcelUtils {

    /**
     * 判断是否为excel文件
     */
    public static boolean isExcel(File file) throws IOException {
        boolean flag = false;
        String fileName = file.getName();
        if (StringUtils.startsWith(fileName, "~")){
            return false;
        }
        FileMagic fileMagic = FileMagic.valueOf(new BufferedInputStream(new FileInputStream(file)));
        if (Objects.equals(fileMagic, FileMagic.OLE2) || Objects.equals(fileMagic, FileMagic.OOXML)){
            flag =  true;
        }
        return flag;
    }
}
